package uiLayer;

import java.util.Scanner;

import controllerLayer.CustomerCtr;
import controllerLayer.SaleCtr;

public class CustomerUI {

	private CustomerCtr cctr;
	private static CustomerUI instance = null;
	private SaleCtr sctr;

	public CustomerUI(){
		cctr = new CustomerCtr();
		sctr = new SaleCtr();
		
	}
	public static CustomerUI getInstance()       
	{
		if(instance == null)
		{
			instance = new CustomerUI();
		}
		return instance;
	}
	
	public void customerMenuStart() {
		boolean exit = false;
		while(!exit){
		int choice = printCustomerMenu();
		switch(choice)
		{
		 case 1: registerCustomer();
         break;
		 case 2: findByID();
         break;
		 case 3: updateCustomer();
         break;
		 case 4: deleteCustomer();
		 break;
		 case 5: printOffers();
		 break;
		 case 6: acceptOffer();
		 break;
		 case 7: goBack();
         break;
		}
		}
	}

	private void acceptOffer() {
		Scanner keyboard = new Scanner(System.in);
		 System.out.println("Customer's CPR Number: ");
	     String id = keyboard.nextLine();
		 System.out.println("Offer's ID: ");
	     int Id = Integer.parseInt(keyboard.nextLine());
	     if(Id == -1)printCustomerMenu();
	     try{
	    	 TimberUI.getInstance().acceptOffer(id, Id);
	     }
	     catch(NullPointerException npe){
	    	 System.out.println("Customer or offer doesn't exist");
	     }
	     
	     }

	private void printOffers() {
		Scanner keyboard = new Scanner(System.in);
		 System.out.println("Customer's CPR Number: ");
	     String id = keyboard.nextLine();
	     try{
	     System.out.println(sctr.printOffers(id));
	     }
	     catch(NullPointerException npe){
	    	 System.out.println("Customer doesn't exist or customer has no offer made yet.");
}
	}

	private void deleteCustomer() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Delete Customer***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        
        cctr.deleteCustomer(id);
		
	}

	private void goBack() {
		OfficeUI.getInstance().run();
		
	}

	private void updateCustomer() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Update Customer***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        System.out.println("Phone Number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Address: ");
        String address = keyboard.nextLine();
        System.out.println("Discount: ");
        double discount = InputParser.getInstance().parseDouble(keyboard.nextLine());
		try{
        cctr.updateCustomer(id,address, phoneNumber, discount);
		}
		catch(NullPointerException npe){
		System.out.println("Customer doesn't exist");
	}
		
	}

	private void findByID() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Find Customer***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        
        System.out.println(cctr.findCustomer(id));
		
	}

	private void registerCustomer() {
		Scanner keyboard = new Scanner(System.in); 
        System.out.print('\f');
        System.out.println("***Register Customer***");
        System.out.println("CPR Number: ");
        String id = keyboard.nextLine();
        System.out.println("Name: ");
        String name = keyboard.nextLine();
        System.out.println("Phone Number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Address: ");
        String address = keyboard.nextLine();
        System.out.println("Discount: ");
        double  discount = InputParser.getInstance().parseDouble(keyboard.nextLine());
        double moneySpent = 0;
		cctr.createCustomer(id, name, address, phoneNumber, moneySpent, discount);
		
	}

	private int printCustomerMenu() {
		Scanner keyboard = new Scanner(System.in);
	        System.out.print('\f');
	        System.out.println("*** Customer Menu ***");
	        System.out.println("1. Register Customer");
	        System.out.println("2. Find Customer");
	        System.out.println("3. Update Customer");
	        System.out.println("4. Delete Customer");
	        System.out.println("5. Print Offers");
	        System.out.println("6. Accept Offer");
	        System.out.println("7. Quit");
	        System.out.println("Your choice #: ");
	        
	        int choice = InputParser.getInstance().parseInt(keyboard.nextLine());
	        return choice;
	}

}
